'''
Created on Feb 13, 2014

@author: yfrank
'''
import Transport.State as State

import Transport.Enums as Enums

import Transport.StateMachine as StateMachine

class Closed(State.State):

    def get_type(self):
        return Enums.PeerState.CLOSE

    def process_event(self, event, data):
        if event == Enums.PeerEvent.CONN:
            self.switch_to_next_state(self.sm.stateWaitInput)
            return True;
        return False

'''
class WaitForInput(State.State):

    def get_type(self):
            return Enums.PeerState.OPEN

    def process_event(self, event, data):
        if event == Enums.PeerEvent.DISC:
            self.switch_to_next_state(self.sm.stateClosed)
            return True;
        if event == Enums.PeerEvent.RECV:
            if (self.peer.parse_workspase(data)):
                self.switch_to_next_state(self.sm.stateWaitRobots)
            else:
                self.switch_to_next_state(self.sm.stateWaitInput)
            return True;

        return False

class WaitRobots(State.State):

    def get_type(self):
        return Enums.PeerState.BUSY

    def process_event(self, event, data):
        if event == Enums.PeerEvent.DISC:
            self.switch_to_next_state(self.sm.stateClosed)
            return True;
        if event == Enums.PeerEvent.RECV:
            if (self.peer.parse_robots(data)):
                self.switch_to_next_state(self.sm.stateWaitSources)
            else:
                self.switch_to_next_state(self.sm.WaitingRobots)
            return True;

        return False

class WaitSources(State.State):

    def get_type(self):
        return Enums.PeerState.BUSY

    def process_event(self, event, data):
        if event == Enums.PeerEvent.DISC:
            self.switch_to_next_state(self.sm.stateClosed)
            return True;
        if event == Enums.PeerEvent.RECV:
            if (self.peer.parse_sources(data)):
                self.switch_to_next_state(self.sm.stateWaitingTargests)
            else:
                self.switch_to_next_state(self.sm.stateWaitSources)
            return True;

        return False

class WaitTargets(State.State):

    def get_type(self):
        return Enums.PeerState.BUSY

    def process_event(self, event, data):
        if event == Enums.PeerEvent.DISC:
            self.switch_to_next_state(self.sm.stateClosed)
            return True;
        if event == Enums.PeerEvent.RECV:
            if (self.peer.parse_targets(data)):
                self.switch_to_next_state(self.sm.selfCalculatingPath)
            else:
                self.switch_to_next_state(self.sm.stateWaitingTargests)
            return True;

            return False
'''

class WaitForInput(State.State):

    def get_type(self):
            return Enums.PeerState.OPEN

    def process_event(self, event, data):
        if event == Enums.PeerEvent.DISC:
            self.switch_to_next_state(self.sm.stateClosed)
            return True;
        if event == Enums.PeerEvent.RECV:
            if (self.sm.peer.parse_input(data)):
                self.switch_to_next_state(self.sm.stateCalculatingPath)
                self.sm.peer.calculate_path()
            else:
                self.switch_to_next_state(self.sm.stateWaitInput)
            return True;

        return False

class CalculatingPath(State.State):

    def get_type(self):
        return Enums.PeerState.BUSY

    def process_event(self, event, data):
        if event == Enums.PeerEvent.DISC:
            self.switch_to_next_state(self.sm.stateClosed)
            return True;
        if event == Enums.PeerEvent.SUCC:
            num_path = data
            self.sm.peer.send_output(num_path)
            self.switch_to_next_state(self.sm.stateWaitInput)
            return True;
        if event == Enums.PeerEvent.FAIL:
            self.sm.peer.send_failure(data)
            self.switch_to_next_state(self.sm.stateWaitInput)
            return True;
        if event == Enums.PeerEvent.SEND:
            return True

        return False


class SimulationPeerStateMacine(StateMachine.StateMachine):
    '''
    classdocs
    '''


    def __init__(self, peer):
        '''
        Constructor
        '''
        self.stateClosed = Closed(self)
        self.stateWaitInput = WaitForInput(self)
        self.stateCalculatingPath = CalculatingPath(self)
        '''
        self.stateWaitRobots = WaitRobots(self)
        self.stateWaitSources = WaitSources(self)
        self.stateWaitTargets = WaitTargets(self)
        '''
        super(SimulationPeerStateMacine,self).__init__(peer,self.stateWaitInput)

